import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import { getBanners } from '@/api/recommend'

export interface IBanners {
  bannerBizType: string
  encodeId: string
  exclusive: boolean
  imageUrl: string
  scm: string
  targetId: number
  targetType: number
  titleColor: string
  typeTitle: string
}

interface IRecommendState {
  banners: IBanners[]
}
const initialState: IRecommendState = {
  banners: []
}

const recommendSlice = createSlice({
  name: 'recommend',
  initialState,
  reducers: {
    changeBannersAction(state, { payload }) {
      state.banners = payload
    }
  }
})

export const fetchBannerDataAction: any = createAsyncThunk(
  'banners',
  async (arg, { dispatch }) => {
    const res = await getBanners()
    dispatch(changeBannersAction(res.banners))
  }
)

export const { changeBannersAction } = recommendSlice.actions

export default recommendSlice.reducer
